import { EntityTypes, Vector3 } from "@minecraft/server";
import { Dimensions } from "@sapi-game/constants";
import { GameComponent } from "@sapi-game/main";
import { RandomUtils } from "@sapi-game/utils";
import { CvSPlayerChicken } from "../context";
import { CvSMainState } from "../mainState";

export class CvSLoader extends GameComponent<CvSMainState> {
    private chicken = EntityTypes.get("minecraft:chicken")!;

    override onAttach(): void {
        const group = this.context.groupSet;
        this.runner.run(async (r) => {
            group.title("地图", this.context.map.name);

            for (let i = 5; i > 0; i--) {
                await r.wait(20);
                group.title(i.toString());
                if (i == 4) {
                    this.spawnChicken(); //生成小鸡
                }
            }
            this.spawnPlayerChicken(); //生成玩家小鸡
            this.loadStructures(); //加载结构
            await r.wait(20);
            this.state.start(); //正式开始
            await r.wait(20);
        });
    }

    loadStructures() {
        this.context.map.structures.forEach((s) => s.place());
    }

    tickingArea(add: boolean) {
        const center = this.context.map.region.center;
        //先删除
        Dimensions.Overworld.runCommand(
            `tickingarea remove ${center.x} ${center.y} ${center.z}`
        );
        if (add) {
            //再添加
            Dimensions.Overworld.runCommand(
                `tickingarea add circle ${center.x} ${center.y} ${center.z} 4`
            );
        }
    }

    spawnChicken(r = 6) {
        const centers = this.context.map.spreadCenters;
        const chickenPerCenter = Math.floor(
            this.context.config.chickenNumber / centers.length
        );
        for (const center of centers) {
            for (let i = 0; i < chickenPerCenter; i++) {
                try {
                    this.spawnSingleChicken(center, r);
                } catch (err) {}
            }
        }
    }

    spawnSingleChicken(center: Vector3, r: number) {
        const point = RandomUtils.randomPointInCircle(center.x, center.z, r);
        const block = Dimensions.Overworld.getBlock({
            x: point[0],
            y: center.y,
            z: point[1],
        });
        let pos = center;
        if (block?.isAir) {
            pos = block.center();
        } else {
            //再检测上面
            const above = block?.above();
            if (above?.isAir) pos = above.center();
        }
        //生成并添加tag
        const chicken = Dimensions.Overworld.spawnEntity(this.chicken, pos);
        chicken.addTag(this.state.chickenTag);
    }

    spawnPlayerChicken() {
        const players = this.context.chicken.getAll();
        for (const player of players) {
            const pos = RandomUtils.choice(this.context.map.spreadCenters)!;
            const chicken = Dimensions.Overworld.spawnEntity(this.chicken, pos);
            chicken.addTag(this.state.chickenTag);
            //添加进入玩家小鸡列表
            this.context.playerChickens.push(
                new CvSPlayerChicken(chicken, player)
            );
        }
    }
}
